#include<iostream>
#include<pthread.h>
#include<vector>
#include<string>
#include<unistd.h>
int cnt=0;
#define NUM 5
pthread_cond_t gcond=PTHREAD_COND_INITIALIZER;
pthread_mutex_t glock=PTHREAD_MUTEX_INITIALIZER;

void* threadrun(void* args)
{
    while(1)
    {
        pthread_mutex_lock(&glock);
        std::string name=(char*)args;
        pthread_cond_wait(&gcond,&glock);
        std::cout<<name<<" 计算："<<cnt<<std::endl;
        cnt++;
        pthread_mutex_unlock(&glock);
    }
}

int main()
{
    std::vector<pthread_t> threads;
    for(int i=0;i<NUM;i++)
    {
        pthread_t tid;
        char* name=new char[64];
        int ret=snprintf(name,64,"thread-%d",i);
        (void)ret;
        int n=pthread_create(&tid,nullptr,threadrun,name);
        if(n!=0)
        continue;
        std::cout<<name<<" created!"<<std::endl;
        threads.push_back(tid);
    }
    sleep(3);
    //每隔一秒唤醒一个进程
     while(1)
     {
        sleep(1);
        pthread_cond_signal(&gcond);
     }

    for(int i=0;i<NUM;i++)
    {
        pthread_join(threads[i],nullptr);
    }
    return 0;
}